package com.suse.service.community.mvc.interceptor;

import cn.hutool.core.util.ObjectUtil;
import com.suse.service.community.entity.User;
import com.suse.service.community.service.MessageService;
import com.suse.service.community.util.UserHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 消息拦截器
 * </p>
 *
 * @author 辜玉彬
 * @since 2022-09-03 21:37
 */
@SuppressWarnings("all")
@Component
public class MessageInterceptor implements HandlerInterceptor {

    @Resource
    private MessageService messageService;

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        User user = UserHolder.getUser();
        if (ObjectUtil.isNull(user)) {
            return;
        }
        if (ObjectUtil.isNull(modelAndView)) {
            return;
        }
        int unreadMessageCount = messageService.getUnreadMessageCount();
        modelAndView.addObject("unreadMessageCount",unreadMessageCount);
    }

}
